package com.gali.lovefinder.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.gali.lovefinder.common.BaseResponse;
import com.gali.lovefinder.common.ErrorCode;
import com.gali.lovefinder.common.ResultUtils;
import com.gali.lovefinder.model.bo.user.UserRegisterBO;
import com.gali.lovefinder.model.bo.user.UserUpdateBO;
import com.gali.lovefinder.model.entity.User;
import com.gali.lovefinder.model.request.user.UserRegisterReq;
import com.gali.lovefinder.service.UserService;
import com.gali.lovefinder.utils.AssertUtil;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


@RestController
@Slf4j
@RequestMapping("/user")
@Api("用户模块")
public class UserController {
    @Resource
    private UserService userService;

    /**
     * 注册
     * 加密
     *
     * @return
     */
    @PostMapping("/register")
    public BaseResponse<Void> register(@RequestBody UserRegisterReq userRegisterReq) {
        //1.参数校验
        AssertUtil.isNull(userRegisterReq.getMessageCode(), ErrorCode.PARAMS_ERROR);
        AssertUtil.isNull(userRegisterReq.getPassword(), ErrorCode.PARAMS_ERROR);
        AssertUtil.isNull(userRegisterReq.getUserCount(), ErrorCode.PARAMS_ERROR);

        //2.参数转换:userRegisterReq=>userRegisterBO
        UserRegisterBO userRegisterBO = new UserRegisterBO();
        BeanUtils.copyProperties(userRegisterReq, userRegisterBO);
        userService.register(userRegisterBO);

        return ResultUtils.success(null);
    }

    /**
     * 发送邮件验证码
     */
    @GetMapping("/getMessageCode")
    public BaseResponse<Void> getMessageCode(@RequestParam("userCount") String userCount, @RequestParam("messageType") String messageType) {
        AssertUtil.isNull(userCount, ErrorCode.PARAMS_ERROR);
        AssertUtil.isNull(messageType, ErrorCode.PARAMS_ERROR);

        userService.getMessageCode(userCount, messageType);

        return ResultUtils.success(null);
    }

    /**
     * login user
     *
     * @param userCount
     * @param password
     * @return
     */
    @PostMapping("/login")
    public BaseResponse<String> login(@RequestParam("userCount") String userCount,
                                      @RequestParam("password") String password) {
        AssertUtil.isNull(userCount, ErrorCode.PARAMS_ERROR);
        AssertUtil.isNull(password, ErrorCode.PARAMS_ERROR);

        String token = userService.login(userCount, password);
        return ResultUtils.success(token);
    }

    /**
     * 更新用户信息
     *
     * @param userUpdateBO
     * @return
     */
    @PostMapping("/update")
    public BaseResponse<Void> update(@RequestBody UserUpdateBO userUpdateBO) {
        User user = new User();
        //todo 放在业务层 判断该用户是否存在
        BeanUtils.copyProperties(userUpdateBO, user);
        userService.updateById(user);
        return ResultUtils.success(null);
    }

    /**
     * 获取用户信息
     *
     * @param userToken
     * @return
     */
    @GetMapping("/getUserInfo")
    public BaseResponse<User> getUserInfo(@RequestParam("userToken") String userToken) {
        AssertUtil.isNull(userToken, ErrorCode.PARAMS_ERROR);
        //todo 放在业务层处理，利用hutool工具类
        Long id = (Long) StpUtil.getLoginIdByToken(userToken);
        User user = userService.getById(id);
        user.setUserPassword("***********");
        return ResultUtils.success(user);
    }

    /**
     * 退出登录
     *
     * @param
     * @return
     */
    @PostMapping("/logout")
    public BaseResponse<Void> logout() {
        StpUtil.logout();
        return ResultUtils.success(null);
    }
    @PostMapping("/UpdatePwd")
    public BaseResponse<Void> updatePwd(@RequestParam("newPwd") String newPwd, @RequestParam("oldPwd") String oldPwd,@RequestParam("id") Long id) {
        AssertUtil.isNull(newPwd, ErrorCode.NULL_ERROR);
        AssertUtil.isNull(oldPwd,ErrorCode.NULL_ERROR);
        AssertUtil.isNull(id, ErrorCode.NULL_ERROR);
        userService.updatePwd(newPwd, oldPwd, id);
        return ResultUtils.success( null );
    }
}
